package cn.utstarcom.pcba_test.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import cn.utstarcom.pcba_test.R;
import cn.utstarcom.pcba_test.utils.LogInFile;

public class MainActivity extends Activity {

    private static final int NEXT_ITEM = 1;


    private LayoutItem layoutItem;
    private LinearLayout contain;
    private int pos = 0;
    private int final_pos = -1;
    private String[] results = new String[4];
    private String[] preDevices = {"屏幕切换测试", "mic测试", "喇叭测试"};


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
                int type=msg.what;
            switch (type) {
                case NEXT_ITEM :
                    pos++;
                    getLayout();
                break;

            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        layoutItem = new LayoutItem();
        layoutItem.initView();
        getLayout();

    }

    /**
     * 初始化控件
     */
    private void initView() {
        contain = (LinearLayout) findViewById(R.id.contain);
    }


    private void getLayout() {

        contain.removeAllViews();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(1920, 1080);
        LogInFile.write("/sdcard/debug.txt", "addView pos:" + pos);
        contain.addView(layoutItem.getView(), layoutParams);
//        layoutItem.autoDealWith();
    }


    class LayoutItem {
        FrameLayout lightbar_layout_01;
        Button pass_submit_01;
        Button fail_submit_01;
        Button lcd_test_01;
        ImageView image_switch_01;
        int lcd_flag = 0;


        private View getView() {
            if (pos > 19 || pos < 0) {
                return null;
            }

            switch (pos) {
                case 0:
                    return lightbar_layout_01;
                case 1:

                case 2:

            }

            return null;
        }


        private void initView() {

            lightbar_layout_01 = (FrameLayout) LayoutInflater.from(
                    MainActivity.this).inflate(
                    R.layout.layout_lightbar_03, null);
            pass_submit_01 = (Button) lightbar_layout_01
                    .findViewById(R.id.pass_submit_01);
            fail_submit_01 = (Button) lightbar_layout_01
                    .findViewById(R.id.fail_submit_01);
            lcd_test_01 = (Button) lightbar_layout_01
                    .findViewById(R.id.lcd_teststart);
            image_switch_01 = (ImageView) lightbar_layout_01.
                    findViewById(R.id.image_switch_01);
            lcd_test_01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    image_switch_01.setVisibility(View.VISIBLE);
                    image_switch_01.setBackgroundColor(Color.WHITE);
                    lcd_flag = 0;
                    pass_submit_01.setVisibility(View.INVISIBLE);
                    fail_submit_01.setVisibility(View.INVISIBLE);
                    lcd_test_01.setVisibility(View.INVISIBLE);
                }
            });
            pass_submit_01.setOnClickListener(new NextItemListener());
            fail_submit_01.setOnClickListener(new failedAndSubmitListener());


            image_switch_01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (lcd_flag == 0) {
                        image_switch_01.setBackgroundColor(Color.RED);
                        pass_submit_01.setVisibility(View.INVISIBLE);
                        fail_submit_01.setVisibility(View.INVISIBLE);
                        lcd_test_01.setVisibility(View.INVISIBLE);
                    }
                    if (lcd_flag == 1) {
                        image_switch_01.setBackgroundColor(Color.BLUE);
                    }
                    if (lcd_flag == 2) {
                        image_switch_01.setBackgroundColor(Color.GREEN);
                    }
                    if (lcd_flag == 3) {
                        pass_submit_01.setVisibility(View.VISIBLE);
                        fail_submit_01.setVisibility(View.VISIBLE);
                        lcd_test_01.setVisibility(View.VISIBLE);
                        image_switch_01.setVisibility(View.INVISIBLE);
                    }
                    lcd_flag++;

                }
            });


        }


    }


    class NextItemListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            v.setEnabled(false);
            switch (id) {
                case R.id.pass_submit_01:
                    setItemResult(1);
                    handler.sendEmptyMessage(NEXT_ITEM);
                    break;


            }
        }
    }


    class failedAndSubmitListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.fail_submit_01:
                    setItemResult(0);
                    handler.sendEmptyMessage(NEXT_ITEM);
                    break;
            }
        }

    }






    private void setItemResult(int pass) {
        final_pos = pos;
        if (pass == 1) {
            results[pos] = preDevices[pos] + "  ------pass";

        } else {
            results[pos] = preDevices[pos] + "  ------fail";

        }


    }
}
